package com.tech.platform.cms.logger.publish;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.system.SystemUtil;
import com.tech.platform.cms.logger.annotation.ApiLog;
import com.tech.platform.cms.logger.event.ApiLogEvent;
import com.tech.platform.cms.logger.model.ApiLogModel;
import com.tech.platform.cms.logger.utils.SpringEventUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author shengqiang.wu
 * @date 2020/3/24/024 17:10
 * @desc
 */
@Component
public class ApiLogPublish {

    private String serviceName = "default";


    public  void publish(ApiLog apiLog, HttpServletRequest request, ProceedingJoinPoint point,long time){

        String moduleName = apiLog.apiModule();
        String apiName = apiLog.apiName();
        String apiUrl = request.getRequestURI();
        String remoteIp = request.getRemoteAddr();
        String hostIp = SystemUtil.getHostInfo().getAddress();
        String className = point.getTarget().getClass().getName();
        String methodName = point.getSignature().getName();
        String params = ServletUtil.getBody(request);

        ApiLogModel model = new ApiLogModel();
        model.setApiName(moduleName+"-"+apiName);
        model.setRequestUrl(apiUrl);
        model.setDealTimes(time);
        model.setClassName(className);
        model.setHost(hostIp);
        model.setMethod(methodName);
        model.setRemoteIp(remoteIp);
        model.setParams(params);
        model.setCreateTime(new Date());
        SpringEventUtils.publishEvent(new ApiLogEvent(model));
    }
}
